﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace GameLibrary
{
    public struct MouseState
    {
        private PointF m_position;
        private MouseButtons m_buttons;

        public MouseState(PointF position, MouseButtons buttons)
        {
            m_position = position;
            m_buttons = buttons;
        }

        public PointF Position { get { return m_position; } }
        public MouseButtons Buttons { get { return m_buttons; } }

        public bool IsButtonDown(MouseButtons button)
        {
            return ((this.Buttons & button) == button);
        }

        public bool IsButtonUp(MouseButtons button)
        {
            return !IsButtonDown(button);
        }
    }

    public class Mouse
    {
        private GamePanel m_hostPanel = null;

        public Mouse(GamePanel hostPanel)
        {
            if (hostPanel == null)
                throw new ArgumentNullException("hostPanel");

            m_hostPanel = hostPanel;
        }

        public MouseState GetState()
        {
            PointF hostLoc = m_hostPanel.PointToScreen(m_hostPanel.Location);
            PointF cursorLoc = GamePanel.MousePosition;

            PointF mouseLoc = new PointF(cursorLoc.X - hostLoc.X, cursorLoc.Y - hostLoc.Y);

            MouseButtons buttons = GamePanel.MouseButtons;
            if (!m_hostPanel.ClientRectangle.Contains((int)mouseLoc.X, (int)mouseLoc.Y))
                buttons = MouseButtons.None;

            if (mouseLoc.X < 0) mouseLoc.X = 0;
            else if (mouseLoc.X > m_hostPanel.Width) mouseLoc.X = m_hostPanel.Width;

            if (mouseLoc.Y < 0) mouseLoc.Y = 0;
            else if (mouseLoc.Y > m_hostPanel.Height) mouseLoc.Y = m_hostPanel.Height;

            return new MouseState(mouseLoc, buttons);
        }
    }
}
